package org.act.neo4j.temporal.demo.vo;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Relationship;

import java.util.*;

/**
 * Created by song on 16-1-22.
 */
public class RoadChain {
    private static Map<String,RoadChain> roadChainMap = new HashMap<>();
    public static RoadChain get(String gridId, String chainId){
        return roadChainMap.get(gridId+":"+chainId);
    }
    public static void setRoadChain(String gridId, String chainId,RoadChain roadChain){
        roadChainMap.put(gridId+":"+chainId,roadChain);
    }

    private static Map<Integer,RoadChain> roadChainMapByIndex = new HashMap<>();
    public static int relationCount = 0;

    public int index;
    private String gridId;
    private String chainId;
    private int length;
    private int type;
    private List<Integer> inIndexList = new ArrayList<>();
    private List<Integer> outIndexList = new ArrayList<>();
    private int inNum = 0;
    private int outNum = 0;
    private int angle;
    public List<RoadChain> inChains = new ArrayList<>();
    public List<RoadChain> outChains = new ArrayList<>();

    private long relationshipId;

    public RoadChain(String line) {
        String[] fields = line.split(",");
        index = Integer.valueOf(fields[0]);
        gridId = fields[1];
        chainId = fields[2];
        index = Integer.valueOf(fields[3]);
        length = Integer.valueOf(fields[4]);
        type = Integer.valueOf(fields[5]);
        inNum = Integer.valueOf(fields[6]);
        outNum = Integer.valueOf(fields[7]);
        angle = Integer.valueOf(fields[8]);
        for(int i=0;i<inNum+outNum;i++){
            if(i<inNum){
                inIndexList.add(Integer.valueOf(fields[11+i*5]));
            }else{
                outIndexList.add(Integer.valueOf(fields[11+i*5]));
            }
        }
        setRoadChain(gridId,chainId,this);
        roadChainMapByIndex.put(index,this);
        relationCount += inNum+outNum;
    }

    public String toString(){
        return "["+index+"]("+gridId+"-"+chainId+")," +
                "LEN "+length+",TYPE "+type+",ANGLE "+angle+"," +
                "IN"+ Arrays.toString(inIndexList.toArray())+",OUT"+Arrays.toString(outIndexList.toArray());
    }

    public void updateNeighbors() {
        for(int i=0;i<inIndexList.size();i++){
            inChains.add(roadChainMapByIndex.get(inIndexList.get(i)));
        }
        for(int i=0;i<outIndexList.size();i++){
            outChains.add(roadChainMapByIndex.get(outIndexList.get(i)));
        }
    }

    public int getOutNum() {
        return outNum;
    }

    public int getInNum() {
        return inNum;
    }

    public void setRelationship(Relationship relationship) {
        this.relationshipId = relationship.getId();
    }

    public Relationship getRelationship(GraphDatabaseService db) {
        return db.getRelationshipById(this.relationshipId);
    }
}
